package org.summerframework.component.security.core.config;

import org.summerframework.component.security.AdminSave;
import org.summerframework.component.security.Constants;
import org.summerframework.component.security.enumeration.LoginTypeEnum;
import org.summerframework.core.util.CheckUtil;
import org.summerframework.core.util.RedirectUrlBuilder;
import org.summerframework.core.util.UrlEncodeUtil;
import org.summerframework.core.util.UrlUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 石超
 * @version v1.0.0
 */
@Getter
@Setter
@ConfigurationProperties("spring.login")
public class LoginProperties {
    private String redirectUrl = "http://www.libawall.com";
    private String loginPath = "/";
    private String logout = "/logout";
    private String loginUrl = "/";
    @NestedConfigurationProperty
    private SinglePoint singlePoint = new SinglePoint();
    private String charset = "UTF-8";
    private String loginStyle = "default";
    private String redirectStyle = "default";
    private Integer defaultExpiringTime = 1800;
    private Integer longExpiringTime = 30 * 24 * 60 * 60;
    private Integer rememberMeTime = 30 * 24 * 60 * 60;
    @NestedConfigurationProperty
    private AdminSave adminSave = new AdminSave(Collections.singletonList(1L));
    private LoginTypeEnum loginType = LoginTypeEnum.NORMAL_FLOW;
    private Map<String, String> jsonResult = new HashMap<>();
    private Map<String, String> pageResult = new HashMap<>();
    private Map<String, String> view = new HashMap<>();
    private Map<Integer, String> oauth = new HashMap<>();
    private Boolean onlyUser;

    public String redirectLogin() {
        return redirectLogin(redirectUrl, null);
    }

    public String redirectLogin(HttpServletRequest request) {
        RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();
        urlBuilder.setScheme(request.getScheme());
        urlBuilder.setServerName(request.getServerName());
        urlBuilder.setPort(request.getServerPort());
        urlBuilder.setContextPath(request.getContextPath());
        urlBuilder.setServletPath(request.getServletPath());
        urlBuilder.setPathInfo(request.getPathInfo());
        urlBuilder.setQuery(request.getQueryString());
        return redirectLogin(urlBuilder.getUrl(), null);
    }

    public String redirectLogin(String redirectUrl) {
        return redirectLogin(redirectUrl, null);
    }

    public String redirectLogin(String redirectUrl, String style) {
        if (redirectUrl == null) {
            redirectUrl = this.redirectUrl;
        }

        //重新设置异常的url
        if (!CheckUtil.isUrl(loginUrl)) {
            if (!loginPath.startsWith("/")) {
                loginUrl = "/" + loginPath;
            } else {
                loginUrl = loginPath;
            }

            loginUrl = UrlUtil.getUrl("default") + loginUrl;
        }


        String sb = loginUrl + (loginUrl.contains("?") ? "&" : "?") +
                Constants.REDIRECT_URL + "=" + UrlEncodeUtil.encodeURL(redirectUrl);

        if (style != null) {
            sb += "&style=" + style;
        }

        if (singlePoint.enable) {
            sb += "&loginType=" + singlePoint.loginType;
            sb += "&json=" + singlePoint.json;
            return singlePoint.url.replace("{redirectUrl}", UrlEncodeUtil.encodeURL(sb));
        }


        return sb;
    }

    @Setter
    @Getter
    public static class SinglePoint {
        private boolean enable = false;
        private String url;
        private Integer loginType = 3;
        private Integer json = 0;
    }
}
